const formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
  
    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  }
  
  const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
  }
  
  function getUserLocation(){
    // var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(res)
        queryWeather(res);
      },
    })
  }

  function queryWeather(res) {
    // res.latitude = 34.74810791015625;
    // res.longitude = 113.59202575683594;
    // var that = this;
    wx.request({
      url: 'https://jisuqgtq.market.alicloudapi.com/weather/query',
      header: {
        "Authorization": "APPCODE 你自己的APPCODE"  //记得修改你自己的APPCODE
      },
      data: {
        'location': res.latitude + "," + res.longitude
      },
      method: "GET",
      success: function (resRequest) {

        console.info("HTTP - GET")
        console.info(resRequest)

        if (resRequest.statusCode === 200 && resRequest.data.status == 0) {
          var result = resRequest.data.result;
          // that.setData({
          //   weather: result
          // });
          wx.setStorageSync("weather", result);
          console.info(result)
        }
      }
    })
  }

  module.exports = {
    formatTime: formatTime,
    getUserLocation: getUserLocation,
    queryWeather: queryWeather
  }
  